package com.xudow.app.dynamicstate_old.module.follow.viewholder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.domain.Config;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.follow.DynamicLongActivity;
import com.xudow.app.dynamicstate_old.module.follow.DynamicLongDetailActivity;
import com.xudow.app.dynamicstate_old.widget.ArticleSpan;
import com.xudow.app.dynamicstate_old.widget.exview.ExGridView;
import com.xudow.app.dynamicstate_old.widget.exview.NetImageAdapter;

import butterknife.BindView;

/**
 * Created by ske on 2016/5/22.
 */
public class PostLongViewHolder extends DynamicContentViewHolder {
    // content
    @BindView(R.id.dynamic_follows_content_long_title)
    TextView longPostTitile;
    @BindView(R.id.dynamic_follows_content_long)
    TextView longPostContent;
    @BindView(R.id.dynamic_follows_content_long_grid)
    ExGridView imgGridView;
    NetImageAdapter imageAdapter;

    public PostLongViewHolder(RecyclerArrayAdapter<Dynamic> adapter, ViewGroup parent) {
        super(adapter, parent, R.layout.dynamic_follows_item_post_long);
        imgGridView.setAdapter(imageAdapter = new NetImageAdapter(parent.getContext()));
        imageAdapter.setNotifyOnChange(false);
    }

    @Override
    public void setData(final Dynamic dynamic) {
        super.setData(dynamic);
        // 设置content
        longPostTitile.setText(dynamic.getTitle());

        String source;
        if (dynamic.getContent().length() > 140)
            source = dynamic.getContent().substring(0, 140);
        else
            source = dynamic.getContent();
        source += "...全文详情";
        SpannableString spannableString = new SpannableString(source);
//        spannableString.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.name_color)), source.length() - 4, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ArticleSpan(getContext()) {
            @Override
            public void onClick(View widget) {
                Intent i = new Intent(getContext(), DynamicLongDetailActivity.class);
                i.putExtra("dynamic", dynamic);
                getContext().startActivity(i);
            }
        }, source.length() - 4, source.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        longPostContent.setMovementMethod(LinkMovementMethod.getInstance());
        longPostContent.setText(spannableString);

        itemView.setOnClickListener(v -> {
            Intent i = new Intent(getContext(), DynamicLongActivity.class);
            i.putExtra("dynamic", dynamic);
            ((Activity) getContext()).startActivityForResult(i, Config.Code_NeedRefresh);
        });

        //图片
        imageAdapter.clear();
        if (!TextUtils.isEmpty(dynamic.getImgpath())) {
            String[] paths = dynamic.getImgpath().split(";");
            imgGridView.setColumnCount(paths.length == 1 ? 2 : 3);
            imageAdapter.addAll(paths);
            imageAdapter.notifyDataSetChanged();
        }
    }
}
